/*
    Project: OCMF (Operant Chamber Management Framework)
    Version: 1.0
    Author: Copyright (C) 2010, Samuel Delaney & Andrew Harvey
    Licence:
        This file is part of OCMF.

            OCMF is free software: you can redistribute it and/or modify
            it under the terms of the GNU General Public License as published by
            the Free Software Foundation, either version 3 of the License, or
            (at your option) any later version.

            OCMF is distributed in the hope that it will be useful,
            but WITHOUT ANY WARRANTY; without even the implied warranty of
            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
            GNU General Public License for more details.

            You should have received a copy of the GNU General Public License
            along with OCMF.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mainwindow.h"

//funny stuff for images - test
#define SCENE_SIZE 500
#include <QGraphicsView>
#include <QGraphicsScene>

#include <qpixmap.h>
#include <qlabel.h>
#include <qlayout.h>
#include <QDesktopWidget>
#include <QPainter>
#include <QImage>


MainWindow::MainWindow()
{
    //Open the application log
    //Logger::OpenLog();

    //Load the configuration file
    //ServerConfig::LoadConfigFile();

    //create a new instance of the ocmf server
    //ocmfServer = new OcmfServer(this);

    //qApp->setStyleSheet("QMainWindow { background-color: black }");
    //qApp->setStyleSheet("QWidget { background-color: black }");

    setCursor( QCursor( Qt::BlankCursor ) );
    //hide the cursor if in PRD

    //QRect _lastScreenRect = QApplication::desktop()->screenGeometry();

    //QString fileName("image.jpg");

    //QPixmap view;
    //view.load(fileName, 0, 0);


    //screen = new QWidget(this);


    //QLabel *myLabel;
    //myLabel = new QLabel(this);
    //myLabel = new QLabel(screen);

    //myLabel->setPixmap(view);


   // myLabel->setGeometry((_lastScreenRect.width()-view.width())/2, (_lastScreenRect.height()-view.height())/2, view.width(), view.height());

/*
    QGridLayout *MainLayout = new QGridLayout(this);
    MainLayout->addItem(new QHBoxLayout,0,0,4,4);
    MainLayout->addItem(new QVBoxLayout, 6,6,4,4);
    MainLayout->addWidget(myLabel,5,4);
    //setCentralWidget(MainLayout);

    //QWidget *widget = new QWidget;
    //widget->setLayout(MainLayout);

    //setCentralWidget(widget);
    //setCentralWidget(myLabel);

*/

  /*  QString fileName("/tmp/image.png");
    QPainter *pngPainter = new QPainter();
    QImage *image = new QImage(QSize(SCENE_SIZE, SCENE_SIZE), QImage::Format_ARGB32);
    // Resolution of the image is 600 dpi by default
    image->setDotsPerMeterX(600*254);
    image->setDotsPerMeterY(600*254);
    pngPainter->begin(image);
    scene.render(pngPainter);
    pngPainter->end();
    image->save(fileName);
    delete pngPainter;
    delete image;*/

    /*
    placeholders
            screentemplate*/

    ScreenTemplate *testTemplate;
    testTemplate = new ScreenTemplate(QString("Test"));

    Placeholder place,place2;
    place.Height = 100;
    place.Width = 100;
    place.X = 0.1;
    place.Y = 0.1;
    testTemplate->Placeholders.push_back(place);
    place2.Height = 100;
    place2.Width = 100;
    place2.X = 0.7;
    place2.Y = 0.7;
    testTemplate->Placeholders.push_back(place2);

    QVector<Image *>  testImages;

    Image *testImage;
    testImage = new Image();
    testImage->FileName = "image2.jpg";
    testImages.push_back(testImage);
    testImages.push_back(testImage);

    SetupScreen(testTemplate);
    DisplayImages(&testImages);

    showFullScreen();
}

MainWindow::~MainWindow()
{
    //delete resources held by ocmf server
    delete ocmfServer;

#ifdef CONFIG
    //save the server config... just in case
    ServerConfig::SaveConfigFile();
#endif
}

void MainWindow::SetupScreen(ScreenTemplate * screenTemplate)
{
    //get the dimensions of the screen
    QRect screenRect = QApplication::desktop()->screenGeometry();

    //???
    screen = new QWidget(this);

    //pointer used for adding push buttons to the screen
    QPushButton * pushButton;

    //iterate over each placeholder
    for(int i = 0; i < screenTemplate->Placeholders.count(); i++)
    {
        //create a new push button
        pushButton = new QPushButton(screen);

        //set appearance style
        pushButton->setFlat(true);
        pushButton->setGeometry((screenRect.width() * screenTemplate->Placeholders.at(i).X), (screenRect.height() * screenTemplate->Placeholders.at(i).Y),screenTemplate->Placeholders.at(i).Width,screenTemplate->Placeholders.at(i).Height);

        //add pointer to vector used for clearing up later
        LabelVector.push_back(pushButton);
    }

    //???
    QLabel *blackLabel;
    blackLabel = new QLabel(screen);
    blackLabel->setGeometry(screenRect.width(), screenRect.height(), 1, 1);
}

void MainWindow::DisplayImages(QVector<Image *> * imageVector)
{
    //iterate over all images passed
    for(int i = 0; i < LabelVector.count(); i++)
    {
        //set the image displayed on the button
        LabelVector.at(i)->setPixmap(QPixmap(imageVector->at(i)->FileName));

        //connect the clicked function to the Do function of the image
        QObject::connect(LabelVector.at(i), SIGNAL(clicked()), imageVector->at(i), SLOT(Do()));
    }
}

void MainWindow::RemoveImages(QVector<Image *> * imageVector)
{
    //iterate over all images passed
    for(int i = 0; i < LabelVector.count(); i++)
    {
        //remove image from the button
        LabelVector.at(i)->setPixmap(NULL);

        //disconnect the click function from the image
        QObject::disconnect(LabelVector.at(i), SIGNAL(clicked()), imageVector->at(i), SLOT(Do()));
    }
}

void MainWindow::keyPressEvent(QKeyEvent * qKeyEvent)
{
    //check whether the key pressed was the escape key
    if(Qt::Key_Escape == qKeyEvent->key())
        //close the widget
        close();
}

